
<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 用一个行列布局 -->
        <el-row type="flex" justify="space-between" align="middle" style="height: 40px">
          <el-col :span="20">
            <span>江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <el-col>操作</el-col>
            </el-row>
          </el-col>
        </el-row>
        <!-- 放置一个el-tree组件 -->
        <el-tree :data="list" :props="defaultProps" :default-expand-all="true">
          <!-- 作用域插槽 data拿到的是每一个子节点的对象 -->
          <!--
      什么时候用到作用域插槽？父组件中如果想使用子组件中的数据进行自定义内容的渲染 (table 单元格数据渲染)
    -->
          <template #default="{ data }">
            <el-row
              type="flex"
              justify="space-between"
              align="middle"
              style="height: 40px; width: 100%;"
            >
              <el-col :span="20">
                <span>{{ data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager }}</el-col>
                  <el-col>
                    <!-- 下拉菜单 element -->
                    <el-dropdown>
                      <span> 操作<i class="el-icon-arrow-down" /> </span>
                      <!-- 下拉菜单 -->
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="add(data.id)">添加子部门</el-dropdown-item>
                        <el-dropdown-item @click.native="edit(data.id)">编辑部门</el-dropdown-item>
                        <el-dropdown-item @click.native="del(data.id)">删除部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
      </el-card>
    </div>
    <add-departments
      ref="addDepart"
      :show-dialog="showDialog"
      :cur-id="curId"
      :all-list="allList"
      @close-dialog="showDialog=false"
      @update-list="hgetDepartments"
    />
  </div>
</template>

<script>
import { getDepartments, delDepartments } from '@/api/departments'
import { transTreeDate } from '@/utils/transTreeDate'
import AddDepartments from './components/add-departments.vue'
export default {
  components: {
    AddDepartments
  },
  data() {
    return {
      list: [],
      defaultProps: {
        label: 'name',
        children: 'children'
      },
      showDialog: false,
      curId: '',
      allList: []
    }
  },
  mounted() {
    this.hgetDepartments()
  },
  methods: {
    async hgetDepartments() {
      const res = await getDepartments()
      this.allList = res.depts
      this.list = transTreeDate(res.depts)
    },
    del(id) {
      this.$confirm('是否删除该部门', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        // 后台删除
        await delDepartments(id)
        // 提示
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
        // 再次获取列表 刷新
        this.hgetDepartments()
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    add(id) {
      this.showDialog = true
      this.curId = id
    },
    edit(id) {
      this.showDialog = true
      this.$refs.addDepart.getDetail(id)
    }
  }
}
</script>

<style>

</style>
